package com.cz.fra.service.impl;

import java.util.List;

import javax.servlet.ServletException;

import com.cz.fra.dao.AccountDao;
import com.cz.fra.dao.CommentDao;
import com.cz.fra.dao.EntityNotFoundException;
import com.cz.fra.dao.PersistenceException;
import com.cz.fra.dao.ReportDao;
import com.cz.fra.dao.impl.JdbcAccountDao;
import com.cz.fra.dao.impl.JdbcCommentDao;
import com.cz.fra.dao.impl.JdbcReportDao;
import com.cz.fra.entity.Filter;
import com.cz.fra.entity.Report;
import com.cz.fra.service.ReportService;

public class ReportServiceImpl implements ReportService {

	ReportDao reportDao = new JdbcReportDao();
	
	CommentDao commentDao = new JdbcCommentDao();
	
	AccountDao accountDao = new JdbcAccountDao();
	
	@Override
	public String addReport(Report report, List<String> img) throws ServletException {
		try {
			reportDao.addReport(report, img);
		} catch (EntityNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (PersistenceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "Success";
	}

	@Override
	public List<Report> getReportList() throws ServletException {
		List<Report> reportList = null;
		try {
			reportList = reportDao.getReportList();
		} catch (EntityNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (PersistenceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return reportList;
	}

	@Override
	public List<Report> getReportListByFilter(Filter filter)
			throws ServletException {
		List<Report> reportList = null;
		
		String username = filter.getUsername();
		if(username != null && !username.equals("")){
			try {
				int userid = accountDao.getUserIdByUsername(username);
				if(userid != -1){
					filter.setUserId(userid);
				}
				else{
					filter.setUsername(null);
				}
			} catch (EntityNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (PersistenceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		try {
			reportList = reportDao.getReportListByFilter(filter);
		} catch (EntityNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (PersistenceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return reportList;
	}

	@Override
	public Report getReportByID(int reportId) throws ServletException {
		Report report = null;
		try {
			report = reportDao.getReportByID(reportId);
		} catch (EntityNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (PersistenceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return report;
	}

	@Override
	public void incrementCommentNum(int reportId) throws ServletException {
		try {
			reportDao.incrementCommentNum(reportId);
		} catch (EntityNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (PersistenceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
